package com.dinstar.linphone;

import android.content.Context;
import android.os.Build;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyCallback;
import android.telephony.TelephonyManager;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

import com.dinstar.linphone.callback.PhoneStateCallback;
import com.dinstar.linphone.callback.RegistrationCallback;

import org.linphone.core.Account;
import org.linphone.core.AccountParams;
import org.linphone.core.Address;
import org.linphone.core.AuthInfo;
import org.linphone.core.Call;
import org.linphone.core.Config;
import org.linphone.core.Core;
import org.linphone.core.CoreListener;
import org.linphone.core.CoreListenerStub;
import org.linphone.core.Factory;
import org.linphone.core.GlobalState;
import org.linphone.core.LogCollectionState;
import org.linphone.core.ProxyConfig;
import org.linphone.core.RegistrationState;
import org.linphone.core.TransportType;

public class LinphoneManager {
//
//    private static final String TAG = LinphoneManager.class.getSimpleName();
//    private static LinphoneManager instance;
//
//    private Context mContext;
//    private Core mCore;
//    private Preferences mPreferences;
//    private PhoneStateCallback phoneStateCallback;
//    private RegistrationCallback registrationCallback;
//    private boolean mIfStart = false;
//    // 手机电话应用是否在通话中
//    private boolean mGsmCallActive = false;
//
//    private Account mAccount;
//    private AuthInfo mAuthInfo;
//
//    private CoreListener coreListener = new CoreListenerStub() {
//        @Override
//        public void onRegistrationStateChanged(@NonNull Core core, @NonNull ProxyConfig proxyConfig, RegistrationState state, @NonNull String message) {
//            switch (state) {
//                case None -> registrationCallback.none();
//                case Ok -> {
//                    mCore.removeListener(this);
//                    mCore.setDefaultAccount(mAccount);
//                    registrationCallback.ok();
//                }
//                case Progress -> registrationCallback.progress();
//                case Failed -> {
//                    mCore.refreshRegisters();
//                    registrationCallback.failed();
//                }
//                case Cleared -> registrationCallback.cleared();
//            }
//        }
//
//        @Override
//        public void onGlobalStateChanged(@NonNull Core core, GlobalState state, @NonNull String message) {
//            switch (state) {
//                case Startup -> Log.i(TAG, "Core has been called to startup");
//                case Ready -> Log.i(TAG, "Core hase been startup ready after called");
//                case On -> Log.i(TAG, " Core has been started and is up and running.");
//                case Shutdown -> Log.i(TAG, "Core has been called to Stop");
//                case Off -> Log.i(TAG, "Core has been stopped");
//            }
//        }
//
//        @Override
//        public void onCallStateChanged(@NonNull Core core, @NonNull Call call, Call.State state, @NonNull String message) {
//            switch (state){
//                case Connected -> Log.i(TAG, "onCallStateChanged Connected");
//            }
//        }
//    };
//
//    private LinphoneManager(Context ctx, RegistrationCallback registrationCallback, PhoneStateCallback phoneStateCallback) {
//        this.mContext = ctx;
//        String absPath = ctx.getFilesDir().getAbsolutePath();
//        Factory factory = Factory.instance();
//        Config config = factory.createConfigWithFactory(absPath + "/.linphonerc", absPath + "/linphonerc");
//        this.mPreferences = new Preferences(ctx, config);
//        this.mCore = factory.createCoreWithConfig(config, ctx);
//        this.registrationCallback = registrationCallback;
//        this.phoneStateCallback = phoneStateCallback;
//    }
//
//    public static LinphoneManager getInstance(Context ctx, RegistrationCallback registrationCallback, PhoneStateCallback phoneStateCallback) {
//        synchronized (LinphoneManager.class) {
//            if (instance == null) {
//                instance = new LinphoneManager(ctx, registrationCallback, phoneStateCallback);
//            }
//            return instance;
//        }
//    }
//
//
//    public void start() {
//        if (!this.mIfStart) {
//            this.mIfStart = true;
//            Log.i(TAG, "[Context] Starting");
//            // 来电铃声
//            this.mCore.setNativeRingingEnabled(true);
//            // 来电振动
//            this.mCore.setVibrationOnIncomingCallEnabled(true);
//            //回声消除
//            this.mCore.setEchoCancellationEnabled(true);
//            //自适应码率控制
//            this.mCore.setAdaptiveRateControlEnabled(true);
////            this.mCore.addListener(coreListener);
//
//            this.mCore.setSessionExpiresValue(90);
//            String sdkVersion = Build.VERSION.RELEASE;
//            this.mCore.setUserAgent("Dinlink/tanlong", sdkVersion);
//            this.mCore.start();
//
//            //设置手机来电监听
////            TelephonyManager telephonyManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
////            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
////                telephonyManager.registerTelephonyCallback(mContext.getMainExecutor(), new InnerCallStateListener() {
////                    @Override
////                    public void onCallStateChanged(int i) {
////                        Log.i(TAG, TelephonyManager.CALL_STATE_IDLE == i ? "手机空闲" : TelephonyManager.CALL_STATE_RINGING == i ? "手机有来电" : "手机接听电话中");
////                        updateGsmCallStatus(i);
////                    }
////                });
////            } else {
////                telephonyManager.listen(new PhoneStateListener() {
////                    @Override
////                    public void onCallStateChanged(int i, String phoneNumber) {
////                        Log.i(TAG, TelephonyManager.CALL_STATE_IDLE == i ? "手机空闲" : TelephonyManager.CALL_STATE_RINGING == i ? "手机有来电" : "手机接听电话中");
////                        updateGsmCallStatus(i);
////                    }
////                }, PhoneStateListener.LISTEN_CALL_STATE);
////            }
//        }
//    }
//
//    public void stop() {
//        this.mIfStart = false;
//        this.mGsmCallActive = false;
//        //设置手机来电监听
////        TelephonyManager telephonyManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
////        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
////            telephonyManager.unregisterTelephonyCallback(new InnerCallStateListener() {
////
////            });
////        } else {
////            telephonyManager.listen(new PhoneStateListener() {
////
////            }, PhoneStateListener.LISTEN_NONE);
////        }
////        mCore.removeListener(coreListener);
//        mCore.stop();
//    }
//
//    public void doRegister(String username, String password, String domain, TransportType t) {
//        String identity = "sip:" + username + "@" + domain;
//        Address address = Factory.instance().createAddress(identity);
//        address.setHeader("User-Agent", "tanlong");
//        this.mAuthInfo = Factory.instance().createAuthInfo(address.getUsername(), null, password.trim(), null, null, address.getDomain());
//        mCore.addAuthInfo(this.mAuthInfo);
//        AccountParams params = this.mCore.createAccountParams();
//        params.setIdentityAddress(address);
//        Address serverAddress = Factory.instance().createAddress("sip:" + domain);
//        params.setServerAddress(serverAddress);
//        this.mAccount = this.mCore.createAccount(params);
//        Log.i(TAG, "Trying to log in account with SIP identity [" + address.asStringUriOnly() + "]");
//        this.mCore.addListener(coreListener);
//        this.mCore.addAccount(this.mAccount);
//    }
//
//    private void updateGsmCallStatus(int i) {
//        this.mGsmCallActive = i == TelephonyManager.CALL_STATE_IDLE;
//    }
//
//    @RequiresApi(api = Build.VERSION_CODES.S)
//    private static class InnerCallStateListener extends TelephonyCallback implements TelephonyCallback.CallStateListener {
//
//        @Override
//        public void onCallStateChanged(int i) {
//        }
//    }

}
